﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BenefitHelper.View
{
    /// <summary>
    /// 交易员日多空数据
    /// </summary>
    public class OperatorDayDirectionInfo
    {
        public List<OperatorDayDirectionInfoData> Datas { get; set; }

        public OperatorDayDirectionInfo GetOperatorDayDirectionInfo(int tradeHistoryId, int operatorid, bool win, DB.DBManager db)
        {
            OperatorDayDirectionInfo data = new OperatorDayDirectionInfo();
            var query = db.OperatorDayDirectionInfoData.Where(a => a.OperatorId == operatorid).Where(a => a.TradeHistoryId == tradeHistoryId);
            data.Datas = query.ToList();
            return data;
        }



        public void InitOperatorDayDirectionInfo(int tradeHistoryId, bool win, DB.DBManager db)
        {
            OperatorDayDirectionInfo info = new OperatorDayDirectionInfo();
            info.Datas = new List<OperatorDayDirectionInfoData>();

            var operators = db.Operators.ToList();
            foreach (Sys.Operator op in operators)
            {
                List<Sys.Account> accounts = new Sys.DayOperatorAccount().GetDayOperatorAccounts(new Sys.TradeHistory().GetDateTimeFromTradeHistoryId(tradeHistoryId, db), op.Id, db);
                foreach (Sys.Account account in accounts)
                {
                    List<Data.Day.AccountDayTradeProduct> infos = new Data.Day.AccountDayTradeProduct().GetAccountDayTradeProduct(account.Id, tradeHistoryId, win, db);
                    foreach (Data.Day.AccountDayTradeProduct i in infos)
                    {
                        List<View.OperatorDayDirectionInfoData> ps = null;
                        if (i.Direction == 1)
                        {
                            ps = info.Datas.Where(a => a.Direction.Equals("空头")).ToList();
                        }
                        else
                        {
                            ps = info.Datas.Where(a => a.Direction.Equals("多头")).ToList();
                        }
                        if (ps.Count > 0)
                        {
                            OperatorDayDirectionInfoData data = ps.First();
                            data.BillCount = data.BillCount + i.BillCount;
                            data.SumValue = data.SumValue + Math.Abs(i.Profit);
                        }
                        else
                        {
                            OperatorDayDirectionInfoData data = new OperatorDayDirectionInfoData();
                            if (i.Direction == 1)
                            {
                                data.Direction = "空头";
                            }
                            else
                            {
                                data.Direction = "多头";
                            }

                            data.BillCount = i.BillCount;
                            data.SumValue = Math.Abs(i.Profit);
                            info.Datas.Add(data);
                        }
                    }
                }
                var allSum = info.Datas.Sum(a => a.SumValue);
                foreach (OperatorDayDirectionInfoData a in info.Datas)
                {
                    a.Percent = Convert.ToInt32((a.SumValue / allSum) * 100);
                    a.OperatorId = op.Id;
                    a.TradeHistoryId = tradeHistoryId;
                    db.OperatorDayDirectionInfoData.Add(a);
                }

            }
            db.SaveChanges();
        }

        public void Delete(int tradeHistoryId, DB.DBManager db)
        {
            var query = db.OperatorDayDirectionInfoData.Where(a => a.TradeHistoryId == tradeHistoryId);
            foreach (View.OperatorDayDirectionInfoData d in query)
            {
                db.OperatorDayDirectionInfoData.Remove(d);
            }
            db.SaveChanges();
        }
    }

    public class OperatorDayDirectionInfoData
    {
        public int Id { get; set; }

        public string Direction { get; set; }

        public double BillCount { get; set; }

        public double SumValue { get; set; }

        public double Percent { get; set; }

        public int OperatorId { get; set; }

        public virtual Sys.Operator Operator { get; set; }

        public int TradeHistoryId { get; set; }

        public virtual Sys.TradeHistory TradeHistory { get; set; }
    }
}
